Skip to content

UCT/CUDA_COPY: Fix incorrect mem_flags detection#11635

Merged
brminich merged 10 commits into
openucx:masterfrom
tomerg-nvidia:fix_no_context_mem_flags
Jul 10, 2026
Merged

UCT/CUDA_COPY: Fix incorrect mem_flags detection#11635
brminich merged 10 commits into
openucx:masterfrom
tomerg-nvidia:fix_no_context_mem_flags

Conversation

@tomerg-nvidia

@tomerg-nvidia tomerg-nvidia commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

What?

Push a cuda context before flag detection if the flag detection is going to export dmabuf fd.
If Memory is host-based vmm, skip the dmabuf fd check and just mark as registerable.

Why?

If there's currently no context and the memory flags detection is called the dmabuf export can fail, making it non-registerable.
Host-based vmm fail dmabuf fd export.

How?

Push context when needed, trying to use available context first

@tomerg-nvidia tomerg-nvidia changed the title Fix no context mem flags UCT/CUDA_COPY: Fix no context mem flags Jul 8, 2026
@tvegas1 tvegas1 self-requested a review July 8, 2026 16:57
Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated
mem_attr->alloc_length = addr_mem_info.alloc_length;
}

/* mem_flags detection requires a current context; push one if needed */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe: DMABUF fd creation/export, also used to detect mem_flags, needs a cuda context

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed it, now the push is much closer to the use

Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated
Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated
Comment on lines +602 to +604
CUcontext tmp_ctx;

UCT_CUDADRV_FUNC_LOG_WARN(cuCtxPopCurrent(&tmp_ctx));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CUcontext tmp_ctx;
UCT_CUDADRV_FUNC_LOG_WARN(cuCtxPopCurrent(&tmp_ctx));
(void)UCT_CUDADRV_FUNC_LOG_WARN(cuCtxPopCurrent(NULL));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant anymore

Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated

UCT_CUDADRV_FUNC_LOG_WARN(cuCtxPopCurrent(&tmp_ctx));
if (cuda_ctx == NULL) {
UCT_CUDADRV_FUNC_LOG_WARN(cuDevicePrimaryCtxRelease(cuda_device));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UCT_CUDADRV_FUNC_LOG_WARN(cuDevicePrimaryCtxRelease(cuda_device));
(void)UCT_CUDADRV_FUNC_LOG_WARN(cuDevicePrimaryCtxRelease(cuda_device));

More often than not, it is explicitly indicated that the return value is ignodred.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not relevant anymore

Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated
Comment on lines +680 to +681
*cuda_ctx_p = NULL;
*cuda_device_p = CU_DEVICE_INVALID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to set values only in case of returning UCS_OK.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c Outdated
Comment on lines +980 to +981
ctx_pushed = (uct_cuda_copy_md_push_ctx(cuda_ctx, cuda_device) ==
UCS_OK);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we return from the function if the method returns an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant anymore. Also, for the dmabuf export the context push is best-effort to not change the current behavior which uses current context

Comment on lines +534 to +537
ctx_status = cuCtxGetCurrent(&cuda_ctx);
if ((ctx_status != CUDA_SUCCESS) || (cuda_ctx != nullptr)) {
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the test really need to check this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

UCS_TEST_P(test_mem_alloc_device, no_current_context_user_mem_registrable,
"CUDA_COPY_ASYNC_MEM_TYPE=cuda")
{
const size_t size = 4 * UCS_MBYTE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constexpr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


mem_attr.field_mask = UCT_MD_MEM_ATTR_V2_FIELD_MEM_TYPE |
UCT_MD_MEM_ATTR_V2_FIELD_MEM_FLAGS;
query_status = uct_md_mem_query_v2(md(), (void*)dptr, size, &mem_attr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast can't do it because it's an integer to pointer conversion, used reinterpret_cast

@tomerg-nvidia tomerg-nvidia force-pushed the fix_no_context_mem_flags branch from f5d17e5 to be6bdd2 Compare July 9, 2026 14:26
@tomerg-nvidia tomerg-nvidia requested review from rakhmets and tvegas1 July 9, 2026 14:49
@tomerg-nvidia tomerg-nvidia changed the title UCT/CUDA_COPY: Fix no context mem flags UCT/CUDA_COPY: Fix incorrect mem_flags detection Jul 10, 2026
@tomerg-nvidia tomerg-nvidia force-pushed the fix_no_context_mem_flags branch from 93c23d0 to 4f88c06 Compare July 10, 2026 07:03
tvegas1
tvegas1 previously approved these changes Jul 10, 2026
Comment thread src/uct/cuda/cuda_copy/cuda_copy_md.c
@brminich brminich merged commit 75a31bc into openucx:master Jul 10, 2026
161 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants